package com.jiuyou.clientController.technician;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.jiuyou.clientController.base.ClientBaseController;
import com.ruoyi.comment.domain.Comment;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.technician.domain.StoreTechnician;
import com.ruoyi.technician.service.IStoreTechnicianService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 *技师相关
 *
 * @author jiuyou
 */
@Api(tags = {"C端技师相关"})
@RestController
@RequestMapping("/client/technician")
public class TechnicianClientController extends ClientBaseController
{
    private static final Logger log = LoggerFactory.getLogger(TechnicianClientController.class);
    @Autowired
    private IStoreTechnicianService storeTechnicianService;

    /**
     * 查询技师信息列表
     */
    @ApiOperation(value = "查询技师信息列表，涉及多分类，建议不分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前第几页",dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize",value = "每页几条数据",dataType = "Integer"),
            @ApiImplicitParam(name = "storeId",value = "门店id",required = true, dataType = "Long"),
            @ApiImplicitParam(name = "techType",value = "技师分类",dataType = "String"),
    })
    @PostMapping("/list")
    public AjaxResult list(Long storeId,String techType,Integer pageNum,Integer pageSize)
    {
        StoreTechnician storeTechnicianParam=new StoreTechnician();
        storeTechnicianParam.setStoreId(storeId);
        storeTechnicianParam.setTechType(techType);
        storeTechnicianParam.setState(DicConstants.SYS_Y.getCode());
        startPage();
        List<StoreTechnician> list = storeTechnicianService.selectStoreTechnicianList(storeTechnicianParam);
        //List<JSONObject> resultList=new ArrayList<JSONObject>();
        TableDataInfo tableDataInfo =getDataTable(list);
        list= (List<StoreTechnician>) tableDataInfo.getRows();
        //不同类型进行分组
        Map<String,JSONObject> techTypeListMap=new HashMap<String,JSONObject>();
        for (StoreTechnician storeTechnician:list) {
            JSONObject jso= tToJson(storeTechnician);


            String tt= storeTechnician.getTechType();
            JSONObject techTypeListJso;
            if(techTypeListMap.containsKey(tt)){
                techTypeListJso=techTypeListMap.get(tt);
            }else{
                techTypeListJso=new JSONObject();
                techTypeListJso.put("rowList",new JSONArray());
                techTypeListMap.put(tt,techTypeListJso);
            }
            JSONArray jsonArray= techTypeListJso.getJSONArray("rowList");
//            techTypeListJso.put("techType", DictUtils.getDictLabel("tech_type",tt));
            jsonArray.add(jso);

        }
        tableDataInfo.setRows(Arrays.asList(techTypeListMap.values().toArray()));
        return AjaxResult.success(tableDataInfo);
    }


    /**
     * 获取技师信息详细信息
     */
    @ApiOperation(value = "获取技师详情")
    @ApiImplicitParam(name = "id",value = "技师id",dataType = "Long")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        StoreTechnician storeTechnician= storeTechnicianService.selectStoreTechnicianById(id);
        JSONObject jso= tToJson(storeTechnician);
        return AjaxResult.success(jso);
    }

    /**
     * 技师对象转为json对象
     * @param storeTechnician
     * @return
     */
    private JSONObject tToJson(StoreTechnician storeTechnician){
        JSONObject jso= new JSONObject();
        jso.put("techImage",storeTechnician.getTechImage());
        jso.put("storeId",storeTechnician.getStoreId());
        jso.put("techBacImage",storeTechnician.getTechBacImage());
        jso.put("techName",storeTechnician.getTechName());
        jso.put("techPhone",storeTechnician.getTechPhone());
        jso.put("techPositional",storeTechnician.getTechPositional());
        jso.put("workYears",storeTechnician.getWorkYears());
        jso.put("goodatAreas",storeTechnician.getGoodatAreas());
        jso.put("techId",storeTechnician.getId());
        return jso;
    }



}
